package com.wx.cloud.word.service.impl;

import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.metadata.Sheet;
import com.wx.cloud.word.common.CommonResult;
import com.wx.cloud.word.mapper.WordMapper;
import com.wx.cloud.word.model.Collect;
import com.wx.cloud.word.model.ExpertWord;
import com.wx.cloud.word.model.Like;
import com.wx.cloud.word.model.Word;
import com.wx.cloud.word.service.WordService;
import com.wx.cloud.word.utils.MyDate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service("wordService")
public class WordServiceImpl implements WordService {

    @Autowired
    private WordMapper wordMapper;

    @Override
    public List<Word> getSelectList(Map<String, Object> data) {
        int page = Integer.parseInt((String) data.get("pageNum"));
        int size = Integer.parseInt((String)data.get("pageSize"));
        data.put("pageNum",(page-1)*size);
        data.put("pageSize",size);
        return wordMapper.getSelectList(data);
    }

    @Override
    public int getSelectCount(Map<String, Object> data) {
        return wordMapper.getSelectCount(data);
    }

    @Override
    public String addWord(Word word) {
        word.setCreateTime(MyDate.getDate());
        int result = wordMapper.addWord(word);
        if (result > 0) {
            return "success";
        }else {
            return "false";
        }
    }

    @Override
    public int delWord(Word word) {
        return wordMapper.delWord(word);
    }

    @Override
    public Like getLikeByData(Map<String, Object> data) {
        return wordMapper.getLikeByData(data);
    }

    @Override
    public String addLike(Like like) {
        like.setCreateTime(MyDate.getDate());
        int result = wordMapper.addLike(like);
        if (result > 0) {
            changeLikeCount(like);
            return "success";
        }else {
            return "false";
        }
    }

    @Override
    public int updateLike(Like like) {
        int result = wordMapper.updateLike(like);
        if (result > 0){
            changeLikeCount(like);
        }
        return result;
    }

    private int changeLikeCount(Like like) {
        int count = wordMapper.getCountByData(like);
        Map param = new HashMap();
        param.put("likeCount",count);
        param.put("id",like.getWordId());
        return wordMapper.updateLikeCount(param);
    }

    @Override
    public Collect getCollectByData(Map<String, Object> data) {
        return wordMapper.getCollectByData(data);
    }

    @Override
    public String addCollect(Collect collect) {
        collect.setCreateTime(MyDate.getDate());
        int result = wordMapper.addCollect(collect);
        if (result > 0) {
            return "success";
        }else {
            return "false";
        }
    }

    @Override
    public int updateCollect(Collect collect) {
        return wordMapper.updateCollect(collect);
    }

    @Override
    public CommonResult createDeletion(List<Integer> ids) throws Exception {
        Map params = new HashMap();
        params.put("ids",ids);
        List<Word> list = wordMapper.getWordList(params);
        List<ExpertWord> expertWords = list.stream().map(word -> {
            ExpertWord expertWord = new ExpertWord();
            expertWord.setId(word.getId());
            expertWord.setWord(word.getWord());
            expertWord.setName(word.getName());
            expertWord.setTranslate(word.getTranslate());
            return expertWord;
        }).collect(Collectors.toList());
        expertWordToExcel(expertWords);
        return CommonResult.success("导出成功");
    }

    public void expertWordToExcel(List<ExpertWord> expertWords) throws Exception{
        //文件输出位置
        OutputStream out = new FileOutputStream("D:/word/test.xlsx");
        ExcelWriter writer = EasyExcelFactory. getWriter(out);
        //写仅有一个Sheet的Excel文件，此场景较为通用
        Sheet sheet = new Sheet(1,0, ExpertWord.class);
        //第一个sheet名称
        sheet.setSheetName("第一个sheet");
        //写数据到write上下文中
        //参数1.创建要写入的模型数据
        //参数2.要写入的目标sheet
        writer.write(expertWords,sheet);
        //将上下文中的最终outputtream写入到指定文件中
        writer .finish();
        //关闭流
        out.close();
    }

    @Override
    public List<Word> getMyWordList(Map<String, Object> data) {
        int page = Integer.parseInt((String) data.get("pageNum"));
        int size = Integer.parseInt((String)data.get("pageSize"));
        data.put("pageNum",(page-1)*size);
        data.put("pageSize",size);
        return wordMapper.getMyWordList(data);
    }

    @Override
    public int getMyWordCount(Map<String, Object> data) {
        return wordMapper.getMyWordCount(data);
    }

    @Override
    public List<Collect> getCollectList(Map<String, Object> data) {
        int page = Integer.parseInt((String) data.get("pageNum"));
        int size = Integer.parseInt((String)data.get("pageSize"));
        data.put("pageNum",(page-1)*size);
        data.put("pageSize",size);
        return wordMapper.getCollectList(data);
    }

    @Override
    public int getCollectCount(Map<String, Object> data) {
        return wordMapper.getCollectCount(data);
    }

}
